home *** CD-ROM | disk | FTP | other *** search
/ Power Bytes: Money & Finance / PowerBytes Money and Finance CD-ROM 01 / PowerBytes Money and Finance CD-ROM 01.iso / Utilities / TransSkel / TransEdit ƒ / DumbEdit.pas next >
Encoding:
Pascal/Delphi Source File  |  1987-01-10  |  10.4 KB  |  458 lines  |  [TEXT/PJMM]

  1. {    DumbEdit - Multiple-window TransEdit Demonstration.}
  2.  
  3. {    The project should include DumbEdit.c (this file), TransEdit.c,}
  4. {    FakeAlert.c, TransSkel.c (or a project made from TransSkel.c)}
  5. {    and MacTraps.}
  6.  
  7. {    28 October 1986        Paul DuBois}
  8.  
  9. {     10 January 1987 Ported to Lightspeed Pascal by Owen Hartnett    }
  10. {    ╜hm Software Co. 163 Richard Drive, Tiverton, RI 02878            }
  11.  
  12.  
  13. PROGRAM DumbEdit;
  14.  
  15.     USES
  16.         TransEditPas, TransSkelPas;
  17.  
  18.     CONST
  19.         maxSize = 8;        { no. font sizes made available }
  20.         hSize = 300;        { horiz, vert size of new windows }
  21.         vSize = 205;
  22.         aboutAlrt = 1000;
  23.  
  24.         { File menu item numbers }
  25.  
  26.         new = 1;                { begin new window }
  27.         open = 2;                { open existing file }
  28.         close = 3;                { close file }
  29.         save = 5;                { save file }
  30.         saveas = 6;            { save under another name }
  31.         saveCopy = 7;        { save a copy w/o switching file binding }
  32.         revert = 8;            { revert to version on disk }
  33.         quit = 10;
  34.  
  35.             { Edit menu item numbers }
  36.  
  37.         undo = 1;
  38.         cut = 3;
  39.         copy = 4;
  40.         paste = 5;
  41.         clear = 6;
  42.  
  43.         { Format menu item numbers }
  44.  
  45.         wordWrap = 1;
  46.         noWrap = 2;
  47.         leftJust = 4;
  48.         centerJust = 5;
  49.         rightJust = 6;
  50.  
  51.     VAR
  52.         lastFront : WindowPtr;    { keeps track of front window }
  53.         fileMenu, editMenu, fontMenu, sizeMenu, formatMenu : MenuHandle;
  54.  
  55.         sizes : ARRAY[0..maxSize] OF integer;
  56.  
  57. {    Uncheck all the items in a menu}
  58.  
  59.  
  60.     PROCEDURE UncheckMenu (theMenu : MenuHandle);
  61.         VAR
  62.             i, nItems : integer;
  63.  
  64.     BEGIN
  65.         nItems := CountMItems(theMenu);
  66.  
  67.         FOR i := 1 TO nItems DO
  68.             BEGIN
  69.                 CheckItem(theMenu, i, false);
  70.                 SetItemStyle(theMenu, i, []);
  71.             END;
  72.     END;
  73.  
  74. {    Set the Font, Size and Format menus so that the items corresponding}
  75. {    to the text characteristics of the window are checked.  If the}
  76. {    window isn't an edit window, dim all three menus.}
  77.  
  78.  
  79.     PROCEDURE SetTextMenus (drawBar : Boolean);
  80.  
  81.         VAR
  82.             theWind : WindowPtr;
  83.             wFontName, mFontName : str255;
  84.             i, nItems : integer;
  85.             te : TEHandle;
  86.  
  87.     BEGIN
  88.         theWind := Frontwindow;
  89.         UncheckMenu(fontMenu);                { toss current check marks }
  90.         UncheckMenu(sizeMenu);
  91.         UncheckMenu(formatMenu);
  92.  
  93.         IF NOT IsEWindow(theWind) THEN            { disable the menus }
  94.             BEGIN
  95.                 DisableItem(fontMenu, 0);
  96.                 DisableItem(sizeMenu, 0);
  97.                 DisableItem(formatMenu, 0);
  98.             END
  99.         ELSE
  100.             BEGIN
  101.                 EnableItem(fontMenu, 0);
  102.                 EnableItem(sizeMenu, 0);
  103.                 EnableItem(formatMenu, 0);
  104.  
  105.                 te := GetEWindowTE(theWind);
  106.                 IF te^^.crOnly < 0 THEN
  107.  
  108. {    Check appropriate word wrap item}
  109.  
  110.                     CheckItem(formatMenu, noWrap, true)
  111.                 ELSE
  112.                     CheckItem(formatMenu, wordWrap, true);
  113.  
  114. {    Check appropriate justification item{}
  115.  
  116.                 CASE te^^.just OF
  117.                     teJustLeft : 
  118.                         CheckItem(formatMenu, leftJust, true);
  119.                     teJustRight : 
  120.                         CheckItem(formatMenu, rightJust, true);
  121.                     teJustCenter : 
  122.                         CheckItem(formatMenu, centerjust, true);
  123.                     OTHERWISE
  124.                 END;
  125.  
  126. {    Check appropriate font name item}
  127.  
  128.                 FOR i := 0 TO maxSize - 1 DO
  129.                     BEGIN
  130.                         IF te^^.txSize = sizes[i] THEN
  131.                             checkitem(sizeMenu, i + 1, true);
  132.                         IF RealFont(te^^.txFont, sizes[i]) THEN
  133.                             SetItemStyle(sizeMenu, i + 1, [outline])
  134.                         ELSE
  135.                             SetItemStyle(sizeMenu, i + 1, []);
  136.                         GetFontName(te^^.txFont, wFontName);
  137.                         nItems := CountMItems(fontMenu);
  138.                     END;
  139.                 FOR i := 1 TO nItems - 1 DO
  140.                     BEGIN
  141.                         GetItem(fontmenu, i, mFontName);
  142.                         IF EqualString(wFontName, mFontName, false, true) THEN
  143.                             CheckItem(fontMenu, i, true);
  144.                     END;
  145.                 IF drawBar THEN
  146.                     DrawMenuBar;
  147.             END;
  148.     END;
  149.  
  150. {    Set File/Edit menu items according to type of front window.}
  151.  
  152. {    The general behavior is:}
  153.  
  154. {    New and Open always enabled, since a new edit window can always be}
  155. {    opened.}
  156.  
  157. {    Close enabled when an edit or DA window in front (i.e., when there's}
  158. {    a window at all).}
  159.  
  160. {    Save enabled for edit windows not bound to a file, and edit windows}
  161. {    bound to a file when they're dirty (typed into, Edit menu used to}
  162. {    do something to them).}
  163.  
  164. {    Save As and Save a Copy As enabled for edit windows.}
  165.  
  166. {    Revert enabled for edit windows bound to a file when they're dirty.}
  167.  
  168. {    Undo disabled when there's an edit window in front.}
  169.  
  170.     PROCEDURE SetNonTextmenus;
  171.  
  172.         VAR
  173.             theWind : WindowPtr;
  174.             theKind : integer;
  175.             thePeek : windowPeek;
  176.  
  177.     BEGIN
  178.         DisableItem(fileMenu, close);    { assume no window at all }
  179.         DisableItem(fileMenu, save);
  180.         DisableItem(fileMenu, saveas);
  181.         DisableItem(fileMenu, savecopy);
  182.         DisableItem(fileMenu, revert);
  183.         EnableItem(editMenu, undo);
  184.  
  185.         theKind := 0;
  186.         theWind := FrontWindow;
  187.         thePeek := WindowPeek(theWind);
  188.         IF theWind <> NIL THEN
  189.             theKind := thePeek^.windowKind;
  190.         IF theKind < 0 THEN                            { DA in front }
  191.             EnableItem(fileMenu, close)
  192.         ELSE IF IsEWindow(theWind) THEN            { edit window in front }
  193.             BEGIN
  194.                 EnableItem(fileMenu, close);
  195.                 EnableItem(fileMenu, saveas);
  196.                 EnableItem(fileMenu, savecopy);
  197.                 IF (GetEWindowFile(theWind, NIL) = false) THEN    { not bound to file }
  198.                     EnableItem(fileMenu, save)
  199.                 ELSE IF IsEWindowDirty(theWind) THEN            { bound - is it dirty? }
  200.                     BEGIN
  201.                         EnableItem(fileMenu, save);
  202.                         EnableItem(fileMenu, revert);
  203.                     END;
  204.                 DisableItem(editMenu, undo);
  205.             END;
  206.     END;
  207.  
  208. {    Background procedure.  Check front window, reset menus if it}
  209. {    changes.  The menu bar doesn't need redrawing by SetTextMenus}
  210. {    if the previous and current front window are either both edit}
  211. {    windows or both not edit windows.  This check eliminates some}
  212. {    needless menu flashing.}
  213.  
  214.     PROCEDURE CheckFront;
  215.  
  216.     BEGIN
  217.         IF FrontWindow <> lastFront THEN
  218.             BEGIN
  219.                 SetNonTextMenus;
  220.                 IF IsEWindow(FrontWindow) = IsEwindow(lastFront) THEN
  221.                     SetTextmenus(false)
  222.                 ELSE
  223.                     SetTextmenus(true);
  224.                 lastFront := FrontWindow;
  225.             END;
  226.     END;
  227.  
  228. {    Got an activate or deactivate.  It doesn't matter which, really.}
  229. {    Set the text menus appropriately for the front window, and draw}
  230. {    the menu bar, as these menus might change state from enabled to}
  231. {    disabled or vice-versa.}
  232.  
  233.  
  234.     PROCEDURE Activate (active : Boolean);
  235.     BEGIN
  236.         CheckFront;
  237.     END;
  238.  
  239. {    Got a keyclick in an edit window.}
  240.  
  241.  
  242.     PROCEDURE Key;
  243.     BEGIN
  244.         SetNonTextMenus;
  245.     END;
  246.  
  247. {    Close selected from File menu, or close box of edit window}
  248. {    clicked.}
  249.  
  250.     PROCEDURE myClose;
  251.         VAR
  252.             theWind : WindowPtr;
  253.             ignore : Boolean;
  254.  
  255.     BEGIN
  256.         GetPort(theWind);
  257.         ignore := EWindowClose(theWind);
  258.         CheckFront;
  259.     END;
  260.  
  261.     PROCEDURE MakeWind (bindToFile : Boolean);
  262.  
  263.         VAR
  264.             r : Rect;
  265.             windCount : integer;
  266.             offset : integer;
  267.             ignore : WindowPtr;
  268.     BEGIN
  269.         windCount := 0;
  270.         IF FrontWindow = NIL THEN
  271.             windCount := 0;
  272.         SetRect(r, 0, 0, hSize, vSize);
  273.         windCount := windCount + 1;
  274.         offset := 50 + (25 * (windCount MOD 4));
  275.         OffsetRect(r, offset, offset);
  276.         ignore := NewEWindow(r, '', true, WindowPtr(-1), true, longint(0), bindToFile);
  277.     END;
  278.  
  279. {    File menu handler}
  280.  
  281.  
  282.     PROCEDURE DoFileMenu (item : integer);
  283.  
  284.         VAR
  285.             theWind : WindowPtr;
  286.             mypeek : WindowPeek;
  287.             ignore : Boolean;
  288.     BEGIN
  289.         theWind := FrontWindow;
  290.         CASE item OF
  291.             new : 
  292.                 MakeWind(false);
  293.             open : 
  294.                 MakeWind(true);
  295.             close : 
  296.                 IF ISEWindow(theWind) THEN
  297.                     ignore := EWindowClose(theWind)
  298.                 ELSE
  299.                     BEGIN
  300.                         mypeek := WindowPeek(theWind);
  301.                         CloseDeskAcc(mypeek^.windowKind);    { DA in front }
  302.                     END;
  303.             save : 
  304.                 ignore := EWindowSave(theWind);
  305.             saveas : 
  306.                 ignore := EWindowSaveAs(theWind);
  307.             revert : 
  308.                 ignore := EWindowRevert(theWind);
  309.             quit : 
  310.                 IF ClobberEWindows = true THEN
  311.                     SkelWhoa;
  312.             OTHERWISE
  313.         END;
  314.         SetNonTextMenus;
  315.     END;
  316.  
  317. {    Handle Font menu items}
  318.  
  319.  
  320.     PROCEDURE DoFontMenu (item : integer);
  321.  
  322.         VAR
  323.             font : integer;
  324.             te : TEHandle;
  325.             theWind : WindowPtr;
  326.             theFontName : Str255;
  327.  
  328.     BEGIN
  329.         theWind := FrontWindow;
  330.         te := GetEWindowTE(theWind);
  331.         IF te <> NIL THEN                { not an edit window }
  332.             BEGIN
  333.                 GetItem(fontMenu, item, theFontName);
  334.                 GetFNum(theFontName, font);
  335.                 SetEWindowSTyle(theWind, font, te^^.txSize, te^^.crOnly, te^^.just);
  336.                 SetTExtMenus(false);
  337.             END;
  338.     END;
  339.  
  340. {    Handle Size menu items }
  341.  
  342.     PROCEDURE DoSizeMenu (item : integer);
  343.  
  344.         VAR
  345.             te : TEHandle;
  346.             theWind : WindowPtr;
  347.     BEGIN
  348.         theWind := FrontWindow;
  349.         te := GetEWindowTE(theWind);
  350.         IF te <> NIL THEN
  351.             BEGIN
  352.                 SetEWindowStyle(theWind, te^^.txFont, sizes[item - 1], te^^.crOnly, te^^.just);
  353.                 SetTextMenus(false);
  354.             END;
  355.     END;
  356.  
  357. {    Handle Format menu items }
  358.  
  359.     PROCEDURE DoFormatMenu (item : integer);
  360.  
  361.         VAR
  362.             font, size, just, wrap : integer;
  363.             te : TEHandle;
  364.             theWind : WindowPtr;
  365.  
  366.     BEGIN
  367.         theWind := FrontWindow;
  368.         te := GetEWindowTE(theWind);
  369.         IF te <> NIL THEN                {  an edit window }
  370.             BEGIN
  371.                 font := te^^.txFont;
  372.                 size := te^^.txSize;
  373.                 just := te^^.just;
  374.                 wrap := te^^.crOnly;
  375.                 CASE item OF
  376.                     wordWrap : 
  377.                         wrap := 0;
  378.                     noWrap : 
  379.                         wrap := -1;
  380.                     leftJust : 
  381.                         just := teJustLeft;
  382.                     centerJust : 
  383.                         just := teJustCenter;
  384.                     rightJust : 
  385.                         just := teJustRight;
  386.                     OTHERWISE
  387.                 END;
  388.                 SetEWindowStyle(theWind, font, size, wrap, just);
  389.                 SetTextMenus(false);
  390.             END;
  391.     END;
  392.  
  393.     PROCEDURE DoAbout;
  394.         VAR
  395.             ignore : integer;
  396.     BEGIN
  397.         ignore := Alert(aboutAlrt, NIL);
  398.     END;
  399.  
  400. {    Initialize TransSkel, create menus and install handlers.}
  401.  
  402.  
  403. BEGIN
  404.     lastFront := NIL;
  405.     sizes[0] := 9;
  406.     sizes[1] := 10;
  407.     sizes[2] := 12;
  408.     sizes[3] := 14;
  409.     sizes[4] := 18;
  410.     sizes[5] := 20;
  411.     sizes[6] := 24;
  412.     sizes[7] := 48;
  413.     SkelInit;
  414.     TransEditInit;
  415.     SkelApple('About DumbEdit...', @DoAbout);
  416.  
  417.     fileMenu := NewMenu(1000, 'File');
  418.     AppendMenu(fileMenu, 'New/N;Open.../O;Close/K;(-;Save/S;Save As...');
  419.     AppendMenu(fileMenu, 'Save a Copy As...;Revert/R;(-;Quit/Q');
  420.     SkelMenu(fileMenu, @DoFileMenu, NIL);
  421.  
  422.     editMenu := NewMenu(1001, 'Edit');
  423.     AppendMenu(editMenu, 'Undo/Z;(-;Cut/X;Copy/C;Paste/V;Clear');
  424.     SkelMenu(editMenu, @EWindowEditOp, NIL);
  425.  
  426.     fontMenu := NewMenu(1002, 'Font');
  427.     DisAbleItem(fontMenu, 0);
  428.     AddREsMenu(fontMenu, 'FONT');
  429.     SkelMenu(fontMenu, @DoFontMenu, NIL);
  430.     sizeMenu := NewMenu(1003, 'Size');
  431.     DisableItem(sizeMenu, 0);
  432.     AppendMenu(sizemenu, '9 Point;10 Point;12 Point;14 Point');
  433.     AppendMenu(sizeMenu, '18 Point;20 Point;24 Point;48 Point');
  434.     SkelMenu(sizeMenu, @DoSizeMenu, NIL);
  435.  
  436.     formatMenu := NewMenu(1004, 'Format');
  437.     DisableItem(formatMenu, 0);
  438.     AppendMenu(formatMenu, 'Word Wrap;No Word Wrap;(-;Left;Center;Right');
  439.     Skelmenu(formatMenu, @DoFormatMenu, NIL);
  440.  
  441.     SetNonTextMenus;
  442.     SetTextMenus(true);
  443.  
  444. {    Do TransEdit-specific setup:  set creator for any files created,}
  445. {    set default text style and event notification procedures for}
  446. {    new windows.}
  447.  
  448.     SetEWindowCreator('DUMB');
  449.     SetEWindowStyle(NIL, monaco, 9, 0, teJustLeft);
  450.     SetEWindowProcs(NIL, @Key, @Activate, @myClose);
  451.  
  452. {    Process events until user quits,}
  453. {    then clean up and exit}
  454.  
  455.     SkelBackground(@CheckFront);
  456.     SkelMain;
  457.     SkelClobber;
  458. END.